<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改应用')" />
    <th:block th:include="include :: ztree-css" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-role-add">
        <h4 class="form-header h4">基本信息</h4>
        <div class="form-group">
            <input name="id" th:value="${id}"  hidden id = "id">
            <label class="col-sm-3 control-label is-required">应用分组：</label>
            <div class="col-sm-8">
                <select class="form-control"  name="longGroup" id = "longGroupSelect" >
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">服务器列表：</label>
            <div class="col-xs-8">
                <select id="appServers" class="form-control select2-multiple" name="appServers" multiple>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">应用名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="appName" id="appName" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">应用编码：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="appCode" id="appCode" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">包名：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="jarName" id="jarName" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">应用路径：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="appPath" id="appPath" required>
            </div>
        </div>


        <div class="form-group">
            <label class="col-sm-3 control-label is-required">日志路径：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="logPath" id="logPath" required>
            </div>
        </div>


        <div class="form-group">
            <label class="col-sm-3 control-label is-required">负责人：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="appPrincipal" id="appPrincipal" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">描述：</label>
            <div class="col-sm-8">
                <input id="groupDes" name="appDesc" class="form-control" type="appDesc">
            </div>
        </div>

        <h4 class="form-header h4">命令管理</h4>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">启动命令：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="startCmd" id="startCmd" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label ">停止命令：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="stopCmd" id="stopCmd" placeholder="停止命令不填写时，默认按照jar包停止">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label ">重启命令：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="restartCmd" id="restartCmd" placeholder="停止命令不填写时，默认按照jar包停止、重启">
            </div>
        </div>



        <h4 class="form-header h4">变量配置</h4>

    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: ztree-js" />
<th:block th:include="include :: select2-js" />
<script type="text/javascript">

    $(function() {
        $.modal.loading("数据加载中...")
        //初始化数据
        $.when(appendServer(),appendGroup(), appendVar(), initFrom()).then(function() {
            console.log("所有方法按顺序执行完毕");
            // $("#form-role-add").find('input, select, textarea, button').prop('disabled', true);
            $.modal.closeLoading();
        });

    });

    function  appendServer() {
        $.operate.get(ctx+"server/listServer",function (res) {
            $.each(res.data, function(index, item) {
                $('#appServers').append('<option value="' + item.id + '">' + item.serverIp+ '('+ item.serverName+  ')</option>');
            });
            $('#appServers').select2({
                placeholder: "请选择服务器",
                allowClear: true
            });
            console.log("服务器初始化完成")
        })
    }

    function appendGroup(callback){
        $.operate.get(ctx+"group/listGroup",function (res) {
            $.each(res.data, function(index, item) {
                $('#longGroupSelect').append('<option value="' + item.id + '">' + item.groupName + '</option>');
            });
            if (callback) callback();
        })
    }

    function appendVar(callback){
        $.operate.get(ctx+"app/listAppVarCols",function (res) {
            $.each(res.data, function(index, item) {
                // 创建新的div元素，动态替换备注为 item.name
                var newDiv = `
        <div class="form-group">
            <label class="col-sm-3 control-label">`+item.name+`：</label>
            <div class="col-sm-8">
                <input  name="`+item.column+`" value="`+item.value+`" class="form-control"  type="text">
            </div>
        </div>`;

                // 将新div元素添加到表单中
                $('#form-role-add').append(newDiv);
            });
            if (callback) callback();
        })
    }


    function initFrom(){
        $.operate.get(ctx+"app/getById?id="+$('#id').val(),function (res) {
            $.each(res.data, function(key, value) {
                console.log("表单初始化开始")

                var element = $('[name="' + key + '"]');
                if(element.length > 0){
                    if(element.is('select') && element.is('[multiple]')){
                        element.val(value.split(",")).trigger('change');
                    }else {
                        element.val(value);
                    }
                }
            });
        })
    }

    $("#form-role-add").validate({
        rules:{
            appName:{
                required:true
            },
            appCode:{
                required:true
            },
            appPrincipal:{
                required:true
            }
        },
        messages: {
            "appName": {
                required: "名称不能为空"
            },
            "appCode": {
                required: "编码不能为空"
            },
            "appPrincipal": {
                remote: "负责人不能为空"
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        if (!$.validate.form()) {
            alert("验证实现")
            return;
        }
        // 获取表单数据并转换为 JSON
        var formData = $("#form-role-add").serializeArray(); // 获取表单字段的数组
        var jsonData = {};
        // 遍历表单数据数组，构建 JSON 对象
        $.each(formData, function() {
            if(jsonData[this.name] === undefined){
                jsonData[this.name] = this.value;
            }else {
                //处理select多选问题
                jsonData[this.name] = jsonData[this.name]+","+this.value;
            }
        });
        $.ajax({
            cache : true,
            type : "POST",
            contentType: "application/json",
            url : ctx + "app/saveData",
            data : JSON.stringify(jsonData),
            async : false,
            error : function(request) {
                $.modal.alertError("系统错误");
            },
            success : function(data) {
                $.operate.successCallback(data);
            }
        });
    }


</script>
</body>
</html>
